$(function(){

    var strReg = "^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$";
    var reg = new RegExp(strReg);

	// input iCheck
    $('input').iCheck({
      checkboxClass: 'icheckbox_square-blue',
      radioClass: 'iradio_square-blue',
      increaseArea: '20%' // optional
    });
    
    $('#toRegister').click(function () {
        layer.open({
            type: 2,
            title: '用户注册',
            maxmin: true,
            shadeClose: true, //点击遮罩关闭层
            area: ['400px', '650px'],
            content: base_url + '/toRegister'
        });
    });

    $('#toReset').click(function () {
        layer.open({
            type: 2,
            title: '密码重置',
            maxmin: true,
            shadeClose: true, //点击遮罩关闭层
            area: ['400px', '500px'],
            content: base_url + '/toReset'
        });
    });

    $('#registerBtn').click(function () {
        $('#usernamediv').removeClass("has-error");
        $('#namediv').removeClass("has-error");
        $('#phonediv').removeClass("has-error");
        $('#usernameMsg').html("");
        $('#nameMsg').html("");
        $('#phoneMsg').html("");
        var username = $('#username').val();
        if (!reg.test(username)) {
            $('#usernamediv').addClass("has-error");
            $('#usernameMsg').html("请输入正确的公司邮箱地址！");
            return;
        }
        var name = $('#name').val();
        if (null == name || name.length == 0) {
            $('#namediv').addClass("has-error");
            $('#nameMsg').html("姓名不能为空！");
            return;
        }
        var phone = $('#phone').val();
        if (null == phone || phone.length == 0) {
            $('#phonediv').addClass("has-error");
            $('#phoneMsg').html("手机号不能为空！");
            return;
        }

        var ii = layer.load();
        $.ajax({
            url: base_url + "/register",
            type: "post",
            async:false,//设置同步方式，非异步！
            cache:false,//严格禁止缓存！
            data: $('#registerForm').serialize(),
            success: function (data) {
                layer.close(ii);
                if ("Success" == data.code) {
                    layer.confirm( "注册成功，登录密码请从邮件中获取" , {
                        icon: 6,
                        title: "系统提示" ,
                        btn: [ "确定" ]
                    }, function(index){
                        layer.close(index);
                        var findex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(findex);
                    });
                } else {
                    layer.alert(data.msg, {icon: 5});
                    return;
                }
            }
        });
    });

    $('#resetBtn').click(function () {
        var mail = $('#mail').val();
        if (!reg.test(mail)) {
            layer.alert("请输入正确的邮箱地址！", {icon: 5});
            return;
        }
        var ii = layer.load();
        $.ajax({
            url: base_url + "/reset",
            type: "post",
            async:false,//设置同步方式，非异步！
            cache:false,//严格禁止缓存！
            data: $('#resetForm').serialize(),
            success: function (data) {
                layer.close(ii);
                if ("Success" == data.code) {
                    layer.confirm( "重置成功，请查看邮箱" , {
                        icon: 6,
                        title: "系统提示" ,
                        btn: [ "确定" ]
                    }, function(index){
                        layer.close(index);
                        var findex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(findex);
                    });
                } else {
                    layer.alert(data.msg, {icon: 5});
                    return;
                }
            }
        });
    });

});

function checkInput() {
    var usernameDiv = $('#usernameDiv');
    var passwordDiv = $('#passwordDiv');
    var usernameMsg = $('#usernameMsg');
    var passwordMsg = $('#passwordMsg');

    var username = $('#username').val();
    var password = $('#password').val();
    if (null == username || username == "") {
        usernameDiv.addClass("has-error");
        usernameMsg.html("登录名不能为空！");
        return false;
    }
    if (null == password || password == "") {
        passwordDiv.addClass("has-error");
        passwordMsg.html("密码不能为空！");
        return false;
    }
    return true;
}

function changeDepName() {
    var depId = $('#depId').val();
    if (null != depId && depId != "") {
        var depName = $('#depId').find("option:selected").text();
        $('#depName').val(depName);
    }
}